<?php


namespace app\engine\controller;


use cmf\controller\HomeBaseController;
use think\Db;

class GoodsController extends HomeBaseController
{
    public function index()
    {
        $data = $this->request->param();
        $res = Db::name('goods_rece')->where(['eid' => $data['id']])->order('createTime desc')->select()->toArray();
        foreach ($res as $k => &$v) {
            $v['leaderName'] = Db::name('leader')->where(['id' => $v['leaderId']])->field(['id,name'])->value('name');
            $v['classes'] = Db::name('leader')->where(['id' => $v['classes']])->value('name');
            $v['sign'] = Db::name('staff')->where(['id' => $v['sign']])->value('name');
        }
        $count = Db::name('goods_rece')->where(['eid' => $data['id'], 'replace' => 1])->select()->toArray();
        $price = 0;
        foreach ($count as $k => &$v) {
            $price += $v['replceNums'] * $v['replcePrice'];
        }
        $this->assign('count', $price);
        $this->assign('id', $data['id']);
        $this->assign('res', $res);
        return $this->fetch();
    }

    public function add()
    {
        $role = Db::name('role_user')->where(['user_id'=>cmf_get_current_admin_id()])->value('role_id');
        if($role == 2 || $role == 3 || $role == 5){
            $this->error('无权限操作');
        }
        $data = $this->request->param();
        $leader = Db::name('leader')->where(['eid' => $data['id']])->field(['id,type'])->select()->toArray();
        $rouster = Db::name('staff')->field(['id', 'name'])->select()->toArray();
        $this->assign('rouster', $rouster);
        $this->assign('leader', $leader);
        $this->assign('id', $data['id']);
        return $this->fetch();
    }

    /**
     * 附带多图上传
     *
     */
    public function doadd()
    {
        $data = $this->request->param();
        if (!isset($data['eid'])) {
            $this->error('缺少关键参数');
        }
        $files = request()->file('imgs');
        unset($data['imgs']);
        if (empty($data['leaderId']) || empty($data['desc']) || empty($data['sign']) || empty($data['adress']) || empty($data['classes']) || empty($data['isSign'])) {
            $this->error('请填写完整');
        }
        if ($files) {
            foreach ($files as $k => $file) {
                // 移动到框架应用根目录/public/uploads/ 目录下
                $info = $file->move(CMF_ROOT . 'public' . '/' . 'uploads');
                if ($info) {
                    $kps[$k] = '/uploads/' . date('Ymd', time()) . '/' . $info->getFilename();
                } else {
                    $this->error($file->getError());
                }
            }
            $data['imgs'] = implode(',', $kps);
        }
        $data['createTime'] = time();
        $res = Db::name('goods_rece')->insert($data);
        if (!$res) {
            $this->error('添加失败');
        } else {
            $this->success('添加成功');
        }
    }

    public function del()
    {
        $role = Db::name('role_user')->where(['user_id'=>cmf_get_current_admin_id()])->value('role_id');
        if($role == 2 || $role ==3 || $role == 5){
            $this->error('无权限操作');
        }
        $data = $this->request->param();
        if (empty($data['id'])) {
            $this->error('缺少关键参数');
        }
        $check = Db::name('goods_rece')->where(['id' => $data['id']])->find();
        $imgFile = explode(',', $check['imgs']);
        foreach ($imgFile as $k => $v) {
            $imgs = CMF_ROOT . $v;
            if ($imgs != CMF_ROOT && file_exists($imgs)) {
                unlink($imgs);
            }
        }
        $res = Db::name('goods_rece')->where(['id' => $data['id']])->delete();
        if (!$res) {
            $this->error('操作失败');
        }
        $this->success('操作成功');
    }

    public function see()
    {
        $data = $this->request->param();
        $id = $data['eid'];
        if (empty($data['id'])) {
            $this->error('缺少关键参数');
        }
        $res = Db::name('goods_rece')->where(['id' => $data['id']])->find();
        $res['imgs'] = explode(',', $res['imgs']);
        foreach ($res['imgs'] as $k => &$v) {
            $v = 'http://' . $_SERVER['SERVER_NAME'] . $v;
        }
        $res['leaderName'] = Db::name('leader')->where(['id' => $res['leaderId']])->value('name');
        $res['signName'] = Db::name('staff')->where(['id' => $res['sign']])->value('name');
        $res['className'] = Db::name('leader')->where(['id' => $res['classes']])->value('name');
        $leader = Db::name('leader')->field(['id,name'])->select()->toArray();
        $rouster = Db::name('staff')->field(['id', 'name'])->select()->toArray();
        $this->assign('id', $id);
        $this->assign('res', $res);
        $this->assign('rouster', $rouster);
        $this->assign('leader', $leader);
        return $this->fetch();
    }

    public function search()
    {
        $data = $this->request->param();
        $id = $data['id'];
        $count = Db::name('goods_rece')->where(['eid' => $data['id'], 'replace' => 1])->select()->toArray();
        $price = 0;
        foreach ($count as $k => &$v) {
            $price += $v['replceNums'] * $v['replcePrice'];
        }
        $where = [];
        if (empty($data['replace']) && !isset($data['replace'])) {
            $where = [];
        } elseif ($data['replace'] == 1) {
            $where['replace'] = 1;
        } elseif ($data['replace'] == 2) {
            $where['replace'] = 2;
        }

        $res = Db::name('goods_rece')->where(['eid' => $data['id']])->where($where)->order('createTime desc')->select()->toArray();
        foreach ($res as $k => &$v) {
            $v['leaderName'] = Db::name('leader')->where(['id' => $v['leaderId']])->field(['id,name'])->value('name');
            $v['classes'] = Db::name('leader')->where(['id' => $v['classes']])->value('name');
            $v['sign'] = Db::name('staff')->where(['id' => $v['sign']])->value('name');
        }
        $this->assign('count', $price);
        $this->assign('res', $res);
        $this->assign('id', $id);
        return $this->fetch();
    }

}